package com.cafuc.discipline.service.impl;

import com.cafuc.discipline.dao.AffairDao;
import com.cafuc.discipline.dao.FileDao;
import com.cafuc.discipline.entity.Affair;
import com.cafuc.discipline.entity.FileToSummary;
import com.cafuc.discipline.respone.Result;
import com.cafuc.discipline.service.AffairService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Mete
 * @createTime 2022/11/27 16:53
 * @description
 */
@Service
@Slf4j
public class AffairServiceImpl implements AffairService {
    @Resource
    AffairDao affairdao;

    @Override
    public Result getAllReport(HashMap<String, Object> params) {
        Result result = new Result();
        int pageSize = Integer.parseInt(params.get("pageSize").toString());
        int pageIndex = Integer.parseInt(params.get("pageIndex").toString());
        params.put("pageBegin",(int)(pageIndex-1)*pageSize);
        params.put("pageSize",pageSize);
        log.info(params.toString());
        /*总数*/
        Map<String,Object> totalStr = affairdao.selectReportCount(params);
        log.info("totalStr："+totalStr.toString());
        if (totalStr==null){
            result.setCode(201);
            result.setMessage("数据为空");
        }else{
            int total = Integer.parseInt(totalStr.get("total").toString());
            if (total==0){
                result.setCode(201);
                result.setMessage("数据为空");
                return result;
            }
            List<Affair> list = affairdao.selectReportAll(params);
            /*查询的数据到最后一页*/
            if((int)(pageIndex-1)*pageSize >=total){
                pageIndex = (total%pageSize);
                result.setMessage("已经到最后一页了");
            }else{
                result.setData(list);
            }
            result.setPage(pageIndex,pageSize,total);

        }
        return result;
    }

    /**
     * 检举类型数据查询
     * @return
     */
    @Override
    public Result selectReportType() {
        List<HashMap<String,Object>> reportList = affairdao.selectReportType(0);
        return Result.success(setJson(reportList));
    }
    /**
     * @Description: 合并检举数据格式
     * @Auther: Mete
     * @Date: 2023/1/6
     */
    private List<HashMap<String,Object>> setJson(List<HashMap<String,Object>> list){
        for(int i=0;i<list.size();i++){
            int id = Integer.parseInt(list.get(i).get("id").toString());
            List<HashMap<String,Object>> nextList = affairdao.selectReportType(id);
            if(nextList.size()!=0){
                nextList = setJson(nextList);
                list.get(i).put("children",nextList);
            }
        }
        return list;
    }
    /**
     * @Description: 新增检举
     * @Auther: Mete 
     * @Date: 2023/3/15
     */
    @Resource
    private FileDao filedao;
    @Override
    public Result insertNewAffair(HashMap<String, Object> params) {
        boolean isAffairOk = affairdao.insertNewAffair(params);
        FileToSummary fileToS = new FileToSummary();
        /*创建文件间距记录关联表记录*/
        String file_id = params.get("file_id").toString();
        if(file_id.isEmpty()||file_id==null){
            /*无附件*/
            if (isAffairOk)
                return Result.success();
        }else{
            /*有附件，插入附件关联信息*/
            fileToS.setFile_id(Integer.parseInt(file_id));
            fileToS.setSummary_id(Integer.parseInt(params.get("id").toString()));
            /*检举记录新增成功后插入附件关联*/
            if (isAffairOk){
                Boolean isOk = filedao.insertFileRelation(fileToS);
                if (isOk)
                    return Result.success();
            }
        }
        return Result.failure("出错");
    }
}
